package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 告知审批制事项清单
 */

public class GaoZhiShengPiZhiShiXiangList extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        AllInfo allInfo = new AllInfo();
        //列表当前页
        int currentPage = 1;
        //列表总页数
        int pags = 0;
        //遍历列表每一页
        while (currentPage != pags) {
            String listUrl = "https://wssp.hainan.gov.cn/wssp3/zwgk/sxInformPromiseListNew?xzqhId=HZ2881f4424539dd0142453c856b0025&pageNumber="+currentPage+"&pageSize=10&isOnLine=&taskName=";
            JSONObject listHeader = new JSONObject();
            listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/gzsp_detail?");
            listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            JSONObject listPaemas = new JSONObject();
            String listStrResponse = getReq(listUrl,listHeader,listPaemas);
            JSONObject listResponse = JSONObject.parseObject(listStrResponse);
                for (Object o : listResponse.getJSONObject("data").getJSONArray("datas")) {
                    JSONObject dept = (JSONObject) o;
                    JSONObject result = new JSONObject();
                    JSONArray datailPageList = new  JSONArray();
                    for (Object o1 : dept.getJSONArray("subdatas")) {
                        JSONObject reqData = (JSONObject) o1;
                        //向详情页信息对象传入请求参数
                        allInfo.setSourcekey(reqData.getString("sourcekey"));
                        allInfo.setId(reqData.getString("1"));
                        //获取封装好的详情页数据
                        allInfo.init();
                        try {
                            datailPageList.add(allInfo.getAllInfo());
                        } catch (Exception e) {
                            break;
                        }
                    }
                    result.put("名称", dept.getString("taskname"));
                    result.put("选项", datailPageList);
                    System.out.println(currentPage+"\n"+result);//测试用代码
                    return processResult(
                            null,
                            null,
                            null,
                            allInfo.getResponce(),
                            result,
                            "海南省",
                            "政务公开-告知审批制事项清单"
                    );
                }
            //更新当前页
            currentPage++;
            //更新总页数
            pags = listResponse.getJSONObject("data").getJSONObject("page").getInteger("pageCount") + 1;
        }
        return null;
    }

    public static void main(String[] args) {
        GaoZhiShengPiZhiShiXiangList g = new GaoZhiShengPiZhiShiXiangList();
        g.getDataDemo();
    }
}
